/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChiTietChuyenBayFrm.java
 *
 * Created on Jun 2, 2011, 8:28:11 PM
 */
package gui;

import dao.ChiTietChuyenBayDAO;
import dao.SanBayDAO;
import dao.ThamSoDAO;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import pojo.ChiTietChuyenBayPOJO;
import pojo.SanBayPOJO;
import pojo.ThamSoPOJO;

/**
 *
 * @author b2
 */
public class ThemChiTietChuyenBayFrm extends javax.swing.JFrame {

    private static String maCB;
    private String[] maSanBay;

    /** Creates new form ChiTietChuyenBayFrm */
    public ThemChiTietChuyenBayFrm(String maCB) {
        initComponents();
        ThemChiTietChuyenBayFrm.maCB = maCB;
    }

    public ThemChiTietChuyenBayFrm() {
        initComponents();
    }

    public void initChiTiet() {
        this.txtMaChuyenBay.setText(maCB);
        if (maCB != null) {
            this.txtMaChuyenBay.setEditable(false);
        }
        ArrayList<SanBayPOJO> ds = SanBayDAO.LayDanhSachSanBay();
        String[] dsSanBay = new String[ds.size()];
        maSanBay = new String[ds.size()];
        for (int i = 0; i < ds.size(); i++) {
            dsSanBay[i] = ds.get(i).getTenSanBay();
            maSanBay[i] = ds.get(i).getMaSanBay();
        }
        DefaultComboBoxModel dcm = new DefaultComboBoxModel(dsSanBay);
        this.jcbSanBayTrungGian.setModel(dcm);
        this.txtThoiGianDung.setText("0");
        this.txtGhiChu.setText("...");
    }

    public boolean kiemtraQD1() {
        boolean kq = true;
        int thoigianDung = Integer.parseInt(this.txtThoiGianDung.getText());
        ThamSoPOJO ts = ThamSoDAO.layDanhSahThamSO();
        //
        ArrayList<ChiTietChuyenBayPOJO> ct = ChiTietChuyenBayDAO.LayDanhSachChiTietCB(maCB);
        if (ts.getSoSanBayTrungGianToiDa() > ct.size()) {
            if (thoigianDung < ts.getThoiGianDungToiThieu() || thoigianDung > ts.getThoiGianDungToiDa()) {
                JOptionPane.showMessageDialog(this, "Sai quy định về thời gian dừng ! Từ " + ts.getThoiGianDungToiThieu() + "đên " + ts.getThoiGianDungToiDa() + "phút");
                kq = false;
            } 
        } else {
            JOptionPane.showMessageDialog(this, "Vượt quá số sân bay trung gian tối đa");
            kq = false;
        }
        return kq;
    }

    public void themChiTiet() {
        String maChuyenBay = this.txtMaChuyenBay.getText();
        int index = this.jcbSanBayTrungGian.getSelectedIndex();
        String maSanBayTG = maSanBay[index];
        int thoigianDung = Integer.parseInt(this.txtThoiGianDung.getText());
        String ghiChu = this.txtGhiChu.getText();
        ChiTietChuyenBayPOJO ct = new ChiTietChuyenBayPOJO();
        ct.setGhiChu(ghiChu);
        ct.setMaChuyenBay(maChuyenBay);
        ct.setThoiGianDung(thoigianDung);
        ct.setSanBayTrungGian(maSanBayTG);
        boolean kq = ChiTietChuyenBayDAO.themChiTietCB(ct);
        if (kq == true) {
            JOptionPane.showMessageDialog(this, "Thêm thành công");
        } else {
            JOptionPane.showMessageDialog(this, "Thêm thất bại");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtMaChuyenBay = new javax.swing.JTextField();
        txtThoiGianDung = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtGhiChu = new javax.swing.JTextArea();
        btnThemMoi = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        btnTaoMoi = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jcbSanBayTrungGian = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Chi tiết chuyến bay");
        setBounds(new java.awt.Rectangle(300, 300, 0, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("CHI TIẾT CHUYẾN BAY");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết chuyến bay"));

        jLabel2.setText("Mã chuyến bay");

        jLabel3.setText("Sân bay trung gian");

        jLabel4.setText("Thời gian dừng");

        jLabel5.setText("Ghi chú");

        txtGhiChu.setColumns(20);
        txtGhiChu.setRows(5);
        jScrollPane1.setViewportView(txtGhiChu);

        btnThemMoi.setText("Thêm mới");
        btnThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemMoiActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnTaoMoi.setText("Tạo mới");
        btnTaoMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTaoMoiActionPerformed(evt);
            }
        });

        jButton1.setText("Thêm sân bay");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(39, 39, 39)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jcbSanBayTrungGian, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtThoiGianDung)
                            .addComponent(txtMaChuyenBay, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE))
                        .addGap(27, 27, 27)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(36, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(btnThemMoi, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                .addGap(37, 37, 37)
                .addComponent(btnThoat, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                .addGap(47, 47, 47)
                .addComponent(btnTaoMoi, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                .addGap(59, 59, 59))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMaChuyenBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jButton1)
                    .addComponent(jcbSanBayTrungGian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtThoiGianDung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnThemMoi)
                        .addComponent(btnThoat))
                    .addComponent(btnTaoMoi))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(140, 140, 140)
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.initChiTiet();
    }//GEN-LAST:event_formWindowOpened

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemMoiActionPerformed
        // TODO add your handling code here:
        boolean kq = this.kiemtraQD1();
        if (kq == true) {
            this.themChiTiet();
        }
    }//GEN-LAST:event_btnThemMoiActionPerformed

    private void btnTaoMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTaoMoiActionPerformed
        // TODO add your handling code here:
        this.initChiTiet();
    }//GEN-LAST:event_btnTaoMoiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                if (maCB != null) {
                    new ThemChiTietChuyenBayFrm(maCB).setVisible(true);
                } else {
                    new ThemChiTietChuyenBayFrm().setVisible(true);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTaoMoi;
    private javax.swing.JButton btnThemMoi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbSanBayTrungGian;
    private javax.swing.JTextArea txtGhiChu;
    private javax.swing.JTextField txtMaChuyenBay;
    private javax.swing.JTextField txtThoiGianDung;
    // End of variables declaration//GEN-END:variables
}
